package br.com.dimensaointerativo.visao;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import br.com.dimensaointerativo.controller.SegurancaController;
import br.com.dimensaointerativo.dao.DAOException;

/**
 * Janela responsável pelo acesso das aulas
 * @author Fabricio Silva
 *
 */
public class Login extends JFrame {
	
	private static final long serialVersionUID = 1L;
	private Container container = getContentPane();
	
	/*
	 * Construtor
	 */
	public Login() {
		super("..::Tela de Login::..");
		container.setLayout(new BorderLayout());
		
		desenharJanela(container);
		
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		setSize(300, 150);
		setVisible(true);
		
		// Centralizando a janela
		setLocationRelativeTo(null);
		
	} //Fim de Login
	
	/**
	 * Responsavel por desenhar a janela referente
	 * a esta classe
	 * @author Fabricio Silva
	 */
	
	public void desenharJanela (final Container c) {
		
		//criando objetos
		JPanel camposInterno = new JPanel(new GridLayout(2, 2));
		JPanel auxCamposInterno = new JPanel(new FlowLayout());
		
		JPanel areaInterna = new JPanel();
		JPanel areaBotoes = new JPanel();
		
		//criando objetos internos
		JLabel lblLogin = new JLabel("Login:  ");
		final JTextField txtLogin = new JTextField(20);
		JPanel pLogin= new JPanel(new FlowLayout(FlowLayout.LEFT));
		pLogin.add(lblLogin);
		pLogin.add(txtLogin);		
		camposInterno.add(pLogin);
		
		JLabel lblSenha = new JLabel("Senha: ");
		final JPasswordField passSenha = new JPasswordField(20);
		JPanel pSenha = new JPanel(new FlowLayout(FlowLayout.LEFT));
		pSenha.add(lblSenha);
		pSenha.add(passSenha);
		camposInterno.add(pSenha);
		
		//ButtonGroup exigido para insersao de JButton
		ButtonGroup grupoBotoes = new ButtonGroup();
		
		JButton bOk = new JButton("Ok");
		JPanel pOk = new JPanel(new FlowLayout(FlowLayout.LEFT));
		bOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ev) {
				SegurancaController sc = new SegurancaController();
				try {
					if (sc.verificarInformacoes(txtLogin.getText(), String.valueOf(passSenha.getPassword()))) {
						setVisible(false);
						ContainerGeral cg = new ContainerGeral();
					}
				} catch (DAOException e) {
					e.printStackTrace();
				}
			}
		});
		grupoBotoes.add(bOk);
		pOk.add(bOk);		
		areaBotoes.add(pOk);
		getRootPane().setDefaultButton(bOk);

		JButton bCancelar = new JButton("Cancelar");
		JPanel pCancelar = new JPanel(new FlowLayout(FlowLayout.LEFT));
		grupoBotoes.add(bCancelar);
		pCancelar.add(bCancelar);
		areaBotoes.add(pCancelar);
		bCancelar.addActionListener(new ActionListener() {  
		    public void actionPerformed(ActionEvent ev) {  
		        System.exit(0);
		    }  
		});  
		pCancelar.add(bCancelar);  
		
		//Configurando layout
		areaInterna.setLayout(new BorderLayout());
		
		//ajustando paineis
		auxCamposInterno.add(camposInterno);
		areaInterna.add(auxCamposInterno, BorderLayout.CENTER);
		
		areaInterna.add(areaBotoes, BorderLayout.SOUTH);
		
		//ajustando container geral
		container.add(areaInterna, BorderLayout.CENTER);
	}
}
